package org.jsola.contract.query;

import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.jsola.contract.constant.AccountType;
import org.jsola.contract.constant.EnterpriseRegType;
import org.jsola.contract.constant.EnterpriseUserType;
import org.jsola.contract.constant.LegalAreaType;
import org.jsola.contract.constant.OrganType;
import org.jsola.contract.constant.PersonAreaType;
import org.jsola.core.query.BaseQuery;
import org.jsola.orm.filter.Order;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import static org.jsola.contract.example.AccountExample.E_ACCOUNT_ID;
import static org.jsola.contract.example.AccountExample.E_ADDRESS;
import static org.jsola.contract.example.AccountExample.E_AGENT_ID_NO;
import static org.jsola.contract.example.AccountExample.E_AGENT_NAME;
import static org.jsola.contract.example.AccountExample.E_COUNTRY;
import static org.jsola.contract.example.AccountExample.E_DEPARTMENT;
import static org.jsola.contract.example.AccountExample.E_EMAIL;
import static org.jsola.contract.example.AccountExample.E_ID_NO;
import static org.jsola.contract.example.AccountExample.E_LEGAL_AREA;
import static org.jsola.contract.example.AccountExample.E_LEGAL_NAME;
import static org.jsola.contract.example.AccountExample.E_MEMBER_ID;
import static org.jsola.contract.example.AccountExample.E_MOBILE;
import static org.jsola.contract.example.AccountExample.E_NAME;
import static org.jsola.contract.example.AccountExample.E_ORGAN;
import static org.jsola.contract.example.AccountExample.E_ORGAN_CODE;
import static org.jsola.contract.example.AccountExample.E_ORGAN_TYPE;
import static org.jsola.contract.example.AccountExample.E_PERSON_AREA;
import static org.jsola.contract.example.AccountExample.E_PROVINCE;
import static org.jsola.contract.example.AccountExample.E_REAL;
import static org.jsola.contract.example.AccountExample.E_REG_TYPE;
import static org.jsola.contract.example.AccountExample.E_SCOPE;
import static org.jsola.contract.example.AccountExample.E_SOURCE;
import static org.jsola.contract.example.AccountExample.E_TITLE;
import static org.jsola.contract.example.AccountExample.E_TYPE;
import static org.jsola.contract.example.AccountExample.E_USER_TYPE;
import static org.jsola.contract.example.AccountExample.N_ACCOUNT_ID;
import static org.jsola.contract.example.AccountExample.N_ADDRESS;
import static org.jsola.contract.example.AccountExample.N_AGENT_ID_NO;
import static org.jsola.contract.example.AccountExample.N_AGENT_NAME;
import static org.jsola.contract.example.AccountExample.N_COUNTRY;
import static org.jsola.contract.example.AccountExample.N_DEPARTMENT;
import static org.jsola.contract.example.AccountExample.N_EMAIL;
import static org.jsola.contract.example.AccountExample.N_ID_NO;
import static org.jsola.contract.example.AccountExample.N_LEGAL_AREA;
import static org.jsola.contract.example.AccountExample.N_LEGAL_NAME;
import static org.jsola.contract.example.AccountExample.N_MEMBER_ID;
import static org.jsola.contract.example.AccountExample.N_MOBILE;
import static org.jsola.contract.example.AccountExample.N_NAME;
import static org.jsola.contract.example.AccountExample.N_ORGAN;
import static org.jsola.contract.example.AccountExample.N_ORGAN_CODE;
import static org.jsola.contract.example.AccountExample.N_ORGAN_TYPE;
import static org.jsola.contract.example.AccountExample.N_PERSON_AREA;
import static org.jsola.contract.example.AccountExample.N_PROVINCE;
import static org.jsola.contract.example.AccountExample.N_REAL;
import static org.jsola.contract.example.AccountExample.N_REG_TYPE;
import static org.jsola.contract.example.AccountExample.N_SCOPE;
import static org.jsola.contract.example.AccountExample.N_SOURCE;
import static org.jsola.contract.example.AccountExample.N_TITLE;
import static org.jsola.contract.example.AccountExample.N_TYPE;
import static org.jsola.contract.example.AccountExample.N_USER_TYPE;

/**
 * 账户信息
 *
 * @author JanusMix
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class AccountQuery extends BaseQuery {

    private static final long serialVersionUID = 1L;

    @ApiParam(value = N_ACCOUNT_ID, example = E_ACCOUNT_ID)
    @Length(max = 32)
    private String accountId;

    @ApiParam(value = N_TYPE, example = E_TYPE, allowableValues = AccountType.VALUES_STR)
    private Integer type;

    @ApiParam(value = N_MEMBER_ID, example = E_MEMBER_ID)
    @Length(max = 32)
    private String memberId;

    @ApiParam(value = N_SOURCE, example = E_SOURCE)
    @Length(max = 50)
    private String source;

    @ApiParam(value = N_PERSON_AREA, example = E_PERSON_AREA, allowableValues = PersonAreaType.VALUES_STR)
    @Length(max = 50)
    private String personArea;

    @ApiParam(value = N_ID_NO, example = E_ID_NO)
    @Length(max = 50)
    private String idNo;

    @ApiParam(value = N_NAME, example = E_NAME)
    @Length(max = 255)
    private String name;

    @ApiParam(value = N_MOBILE, example = E_MOBILE)
    @Length(max = 45)
    private String mobile;

    @ApiParam(value = N_EMAIL, example = E_EMAIL)
    @Length(max = 255)
    private String email;

    @ApiParam(value = N_ORGAN, example = E_ORGAN)
    @Length(max = 255)
    private String organ;

    @ApiParam(value = N_TITLE, example = E_TITLE)
    @Length(max = 100)
    private String title;

    @ApiParam(value = N_ADDRESS, example = E_ADDRESS)
    @Length(max = 255)
    private String address;

    @ApiParam(value = N_COUNTRY, example = E_COUNTRY)
    @Length(max = 50)
    private String country;

    @ApiParam(value = N_PROVINCE, example = E_PROVINCE)
    @Length(max = 50)
    private String province;

    @ApiParam(value = N_DEPARTMENT, example = E_DEPARTMENT)
    @Length(max = 30)
    private String department;

    @ApiParam(value = N_ORGAN_TYPE, example = E_ORGAN_TYPE, allowableValues = OrganType.VALUES_STR)
    private Integer organType;

    @ApiParam(value = N_USER_TYPE, example = E_USER_TYPE, allowableValues = EnterpriseUserType.VALUES_STR)
    private Integer userType;

    @ApiParam(value = N_ORGAN_CODE, example = E_ORGAN_CODE)
    @Length(max = 30)
    private String organCode;

    @ApiParam(value = N_LEGAL_NAME, example = E_LEGAL_NAME)
    @Length(max = 20)
    private String legalName;

    @ApiParam(value = N_LEGAL_AREA, example = E_LEGAL_AREA, allowableValues = LegalAreaType.VALUES_STR)
    private Integer legalArea;

    @ApiParam(value = N_AGENT_NAME, example = E_AGENT_NAME)
    @Length(max = 20)
    private String agentName;

    @ApiParam(value = N_AGENT_ID_NO, example = E_AGENT_ID_NO)
    @Length(max = 50)
    private String agentIdNo;

    @ApiParam(value = N_SCOPE, example = E_SCOPE)
    @Length(max = 255)
    private String scope;

    @ApiParam(value = N_REG_TYPE, example = E_REG_TYPE, allowableValues = EnterpriseRegType.VALUES_STR)
    @Length(max = 20)
    private String regType;

    @ApiParam(value = N_REAL, example = E_REAL)
    private Boolean real;



    @ApiParam(value = "排序字段", allowMultiple=true, type="Order" ,example = "\"orders\": [{\"property\": \"userName\",\"desc\": false}]")
    private List<Order> orders;

    /**
     * 每页的条数
     */
    @ApiParam(value = "分页参数，每页的条数", example = "20")
    @Max(value = 1000, message = "pageSize不能大于1000")
    private Integer pageSize = 20;

    /**
     * 当前第几页
     */
    @ApiParam(value = "分页参数，第几页", example = "1")
    @Min(value = 1, message = "pageNo不能小于1")
    private Integer pageNo = 1;



    /**
     * 添加排序
     * @param propertyName 属性名
     * @param desc 是否降序
     * @return this
     */
    public AccountQuery addOrder(String propertyName, boolean desc) {
        if (CollectionUtils.isEmpty(this.orders)) {
            this.orders = new ArrayList<>();
        }
        this.orders.add(new Order(propertyName,desc));
        return this;
    }

}

